package ui;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.DateField;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import manager.AgentManagerMIDlet;


import java.util.*;

public class AgentCreationScreen extends Form implements CommandListener{
	    private final AgentManagerMIDlet midlet;
	    private final Command exitCommand;
	    private final Command confirmCommand;
	    private final Command receiverModeCommand;
	    private final Command myCommand2;
	    private final Display display;
	    private final ChoiceGroup cgAgentCreation;
	    private int replyIndex;
	    private int choiceGroupIndex;
	    private TextField tfNum;


	    public AgentCreationScreen(AgentManagerMIDlet midlet, String title, Display display)
	    {
	        super(title);
	        this.display = display;
	        this.midlet = midlet;
	        exitCommand = new Command("Exit", Command.EXIT, 1);
	        cgAgentCreation = new ChoiceGroup("Which agent do you want to create ? ", Choice.EXCLUSIVE);
	        cgAgentCreation.append("Meeting Agent", null); //null is for picture
	        cgAgentCreation.append("Agent Number 2", null);
	        cgAgentCreation.append("Agent Number 3", null);
	        
	        cgAgentCreation.setSelectedIndex(replyIndex, true);
	        choiceGroupIndex = this.append(cgAgentCreation);
	        receiverModeCommand = new Command("Receiver Mode", Command.SCREEN, 2);
	        myCommand2 = new Command("myCommand2", Command.SCREEN, 3);
	        confirmCommand = new Command("Confirm", Command.SCREEN,1);
	        
	        this.addCommand(confirmCommand);
	        this.addCommand(exitCommand);
	        this.addCommand(receiverModeCommand);
	        this.addCommand(myCommand2);
	        this.setCommandListener(this);
	    }


	    public void commandAction(Command c, Displayable d)
	    {
	        if (c == exitCommand)
	        {
	            midlet.exitRequested();
	        }else if(c == receiverModeCommand){
	        	IdleScreen idleScreen = new IdleScreen(midlet,display);
	        	display.setCurrent(idleScreen);
	        	//System.out.println("myCommand1 here");
	        	//MeetingAgentEntryScreen meetingAgentEntryScreen = new MeetingAgentEntryScreen(midlet,"MeetingAgentEntry",1);
	        	//display.setCurrent(meetingAgentEntryScreen);
	        //	midlet.displayNext();
	        }else if(c ==confirmCommand){
	        	System.out.println("confirmCommand here");
	        	if(cgAgentCreation.getSelectedIndex()==0){
	        		if(tfNum==null){
	        			tfNum = new TextField("Number of times to choose from : ", "" ,5,TextField.NUMERIC);
	        			this.append(tfNum);
	        		}else if(tfNum.getString().equals("")){

	        		}else{
		        		MeetingAgentEntryScreen meetingAgentEntryScreen = 
		        			new MeetingAgentEntryScreen(midlet,display,"MeetingAgentEntry",1,Integer.parseInt(tfNum.getString()),null);
			        	display.setCurrent(meetingAgentEntryScreen);
	        		}
	        		display.setCurrentItem(tfNum);
	        	}
	        }
	    }
}